"""
异常检测模块API路由
"""
from fastapi import APIRouter, Query, Path
from datetime import datetime
from app.core.response import create_response
from app.services.mock_data import mock_generator

router = APIRouter()


@router.get("", summary="获取完整数据")
async def get_anomaly_detection_data():
    """
    获取异常检测模块的所有数据
    """
    data = mock_generator.generate_anomaly_detection_data()
    return create_response(data=data)


@router.get("/stats", summary="获取实时异常统计")
async def get_anomaly_stats():
    """
    获取异常检测实时统计
    """
    full_data = mock_generator.generate_anomaly_detection_data()
    data = full_data["anomalyStats"]
    return create_response(data=data)


@router.get("/micro", summary="获取微观异常")
async def get_micro_anomalies():
    """
    获取微观层面的个体异常列表
    """
    full_data = mock_generator.generate_anomaly_detection_data()
    data = full_data["microAnomalies"]
    return create_response(data=data)


@router.get("/meso", summary="获取中观异常")
async def get_meso_anomalies():
    """
    获取中观层面的群体异常列表
    """
    full_data = mock_generator.generate_anomaly_detection_data()
    data = full_data["mesoAnomalies"]
    return create_response(data=data)


@router.get("/timeseries", summary="获取时间序列")
async def get_timeseries(
    period: str = Query(
        default="24h", regex="^(24h|7d|30d)$", description="时间段: 24h, 7d, 30d"
    )
):
    """
    获取异常检测时间序列数据

    Args:
        period: 时间段,可选值: 24h(24小时), 7d(7天), 30d(30天)
    """
    full_data = mock_generator.generate_anomaly_detection_data()
    data = full_data["timeSeriesData"]
    return create_response(data=data)


@router.get("/types", summary="获取异常类型分布")
async def get_anomaly_types():
    """
    获取各类异常的分布统计
    """
    full_data = mock_generator.generate_anomaly_detection_data()
    data = full_data["anomalyTypes"]
    return create_response(data=data)


@router.get("/confidence", summary="获取置信度分布")
async def get_confidence_distribution():
    """
    获取异常检测置信度分布
    """
    full_data = mock_generator.generate_anomaly_detection_data()
    data = full_data["confidenceDistribution"]
    return create_response(data=data)


@router.get("/features/micro", summary="获取微观特征重要性")
async def get_micro_feature_importance():
    """
    获取微观异常检测的特征重要性
    """
    full_data = mock_generator.generate_anomaly_detection_data()
    data = full_data["microFeatureImportance"]
    return create_response(data=data)


@router.get("/features/meso", summary="获取中观特征重要性")
async def get_meso_feature_importance():
    """
    获取中观异常检测的特征重要性
    """
    full_data = mock_generator.generate_anomaly_detection_data()
    data = full_data["mesoFeatureImportance"]
    return create_response(data=data)


@router.put("/anomalies/{anomaly_id}/handle", summary="标记异常已处理")
async def handle_anomaly(
    anomaly_id: str = Path(..., description="异常ID,如MA-1001或GA-101")
):
    """
    标记某个异常已被处理

    Args:
        anomaly_id: 异常唯一标识符
    """
    data = {
        "anomalyId": anomaly_id,
        "handled": True,
        "handledAt": datetime.now().isoformat(),
    }
    return create_response(data=data, message="异常已标记为处理")
